package taskadvanced.core.rtm;

import org.eclipse.jface.preference.IPreferenceStore;

import taskadvanced.Activator;
import taskadvanced.preferences.PreferenceConstants;

public class Lists {
	private Service service;
	
	public Lists(Service service){
		this.service = service;
	}
	
	public boolean listExists(String listName){
		service.clearParameters();
		service.addParameter("method", "rtm.lists.getList");
		String result = service.makeQuery("count(//list[@name='" + listName + "'])");		
		return (Integer.parseInt(result) > 0);
	}
	
	// Checks if default list exists
	public boolean listExists(){		
		return listExists(getDefaultList());
	}

	public void createList(String listName){
		String timeline = service.getTimeline();
		service.clearParameters();
		service.addParameter("method", "rtm.lists.add");
		service.addParameter("timeline", timeline);
		service.addParameter("name", listName);
		service.makeQuery();
	}
	
	// Creates default list
	public void createList(){
		createList(getDefaultList());
	}
	
	public String getDefaultList(){
		IPreferenceStore prefs = Activator.getDefault().getPreferenceStore();
		return prefs.getString(PreferenceConstants.DEFAULT_LIST);
	}
}
